package leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2021-12-14 13:33
 **/
public class LeetCode56 {

    public int[][] merge(int[][] intervals) {
        Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
        List<int[]> list = new ArrayList<>();
        int len = intervals.length;
        for (int i = 0; i < len; ++i) {
            int l = intervals[i][0];
            int r = intervals[i][1];
            int size = list.size();
            if (size == 0 || list.get(size - 1)[1] < l) {
                list.add(new int[]{l, r});
            } else {
                list.get(size - 1)[1] = Math.max(list.get(size - 1)[1], r);
            }
        }
        return list.toArray(new int[list.size()][]);
    }


}
